﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;

namespace 功能测试软件.View
{
    /// <summary>
    /// SetView.xaml 的交互逻辑
    /// </summary>
    public partial class SetView : Window
    {
        private ViewModel.SetViewModel viewModel = new ViewModel.SetViewModel();
        private BLL.SetViewBLL setViewBLL;
        public SetView()
        {
            try
            {
                HandyControl.Tools.ConfigHelper.Instance.SetLang("zh-cn");
                InitializeComponent();
                setViewBLL = new BLL.SetViewBLL();
                this.DataContext = viewModel;
                setViewBLL.InitView(viewModel);
                setViewBLL.InitPorts(viewModel);
            }
            catch (Exception ex)
            {
                HandyControl.Controls.MessageBox.Error(ex.Message);
                BtLabelTool.LogHelper.WriteLog(ex.ToString());
            }

        }

        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            try
            {
                //使能显示
                setViewBLL.PicViewInit(viewModel);
            }
            catch (Exception ex)
            {
                HandyControl.Controls.MessageBox.Error(ex.Message);
                BtLabelTool.LogHelper.WriteLog(ex.ToString());
            }
        }

        private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            try
            {
                //使能显示
                setViewBLL.PicViewInit(viewModel);
            }
            catch (Exception ex)
            {
                HandyControl.Controls.MessageBox.Error(ex.Message);
                BtLabelTool.LogHelper.WriteLog(ex.ToString());
            }
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            try
            {
                this.DialogResult = false;
                var result = HandyControl.Controls.MessageBox.Ask("确定保存配置?", "提示");
                if (result == MessageBoxResult.OK)
                {
                    bool check = setViewBLL.PortCheck(viewModel);
                    if (check == false)
                    {
                        HandyControl.Controls.MessageBox.Error("保存配置失败\r\n端口号重复", "错误");
                        e.Cancel = true;
                        return;
                    }
                    setViewBLL.ChangeConfig(viewModel);
                    setViewBLL.SaveConfigModel(TestView.config);
                    this.DialogResult = true;
                }
            }
            catch (Exception ex)
            {
                HandyControl.Controls.MessageBox.Error(ex.Message);
                BtLabelTool.LogHelper.WriteLog(ex.ToString());
            }
        }
    }
}
